package com.yj.springbootjava8.stream;

import java.util.*;
import java.util.stream.Collectors;

public class Java8_Stream01 {

    private static List<Person> personList = null;

    static {
        personList = new ArrayList<>();
        personList.add(new Person("Tom", 8900, 22,"male", "New York"));
        personList.add(new Person("Jack", 7000, 27,"male", "Washington"));
        personList.add(new Person("Lily", 7000, 25,"female", "Washington"));
        personList.add(new Person("Anni", 8200, 30,"female", "New York"));
        personList.add(new Person("Owen", 9500, 35,"male", "New York"));
        personList.add(new Person("Alisa", 7900, 26,"female", "New York"));
    }


    public static void main(String[] args) {
        //1、筛选出员工中工资高于8000的人的姓名，并形成新的集合，形成信集合依赖collect
        List<String> names = personList.stream().filter(person -> {
            return person.getSalary() > 8000;
        }).map(person -> {return person.getName();}).collect(Collectors.toList());
        System.out.println(names);
        //2、获取员工工资最高的人
        Person person = personList.stream().max(Comparator.comparingInt(Person::getSalary)).get();
        System.out.println("员工工资最大值："+person.getSalary());
        //3、将员工的薪资全部增加1000
        List<Person> collect = personList.stream().map(person1 ->{
            person1.setSalary(person1.getSalary() + 1000);
            return person1;
        }).collect(Collectors.toList());
        System.out.println(collect);
        System.out.println(personList);

//
//        // 不改变原来员工集合的方式
//        List<Person> personListNew = personList.stream().map(person2 -> {
//            Person personNew = new Person(person2.getName(), 0, 0, null, null);
//            personNew.setSalary(person2.getSalary() + 10000);
//            return personNew;
//        }).collect(Collectors.toList());
//        System.out.println("一次改动前：" + personList.get(1).getName() + "-->" + personList.get(1).getSalary());
//        System.out.println("一次改动后：" + personListNew.get(1).getSex() + "-->" + personListNew.get(1).getSalary());

        //4、求所有员工的工资之和和最高工资
        Optional<Integer> reduce = personList.stream().map(Person::getSalary).reduce((integer, integer2) -> integer + integer2);
        System.out.println(reduce.get());
        Optional<Integer> reduce1 = personList.stream().map(Person::getSalary).reduce(Integer::sum);
        System.out.println(reduce1.get());

        Integer reduce2 = personList.stream().reduce(0, (sum, person1) -> sum += person1.getSalary(), (integer, integer2) -> {
            return integer + integer2;
        });
        System.out.println(reduce2);

        Integer integer = personList.stream().map(Person::getSalary).max((o1, o2) -> o1 - o2).get();
        System.out.println(integer);

        Integer maxSalary = personList.stream().reduce(0,(max, person1) -> max>person1.getSalary()?max : person1.getSalary(),Integer::sum);
         maxSalary = personList.stream().reduce(0, (max, p) -> max > p.getSalary() ? max : p.getSalary(),
                Integer::max);

         //5、统计员工人数、平均工资、工资总额、最高工资
        Long collect1 = personList.stream().collect(Collectors.counting());//统计员工人数
        System.out.println(collect1);
        Double collect2 = personList.stream().collect(Collectors.averagingDouble(Person::getSalary));
        System.out.println(collect2);
        Integer collect3 = personList.stream().collect(Collectors.summingInt(Person::getSalary));
        System.out.println(collect3);
        DoubleSummaryStatistics collect4 = personList.stream().collect(Collectors.summarizingDouble(Person::getSalary));
        System.out.println(collect4);

        String collect5 = personList.stream().map(person1 -> person1.getName()).collect(Collectors.joining(","));
        System.out.println(collect5);

        System.out.println("按照工资升序排序");
        List<Person> collect6 = personList.stream().sorted(Comparator.comparing(Person::getSalary)).collect(Collectors.toList());
        System.out.println(collect6);
        System.out.println("按照工资降序排序");
        collect6 = personList.stream().sorted(Comparator.comparing(Person::getSalary).reversed()).collect(Collectors.toList());
        System.out.println(collect6);
        System.out.println("先按工资再按照年龄升序");
        List<Person> collect7 = personList.stream().sorted(Comparator.comparing(Person::getSalary).thenComparing(Person::getAge)).collect(Collectors.toList());
        System.out.println(collect7);
        System.out.println("先按工资再按照年龄降序");
        List<Person> collect8 = personList.stream().sorted((o1, o2) -> {
            if (o1.getSalary() == o2.getSalary()) {
                return o2.getAge() - o1.getAge();
            } else {
                return o1.getSalary() - o2.getSalary();
            }
        }).collect(Collectors.toList());
        System.out.println(collect8);

    }
}
